/*
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.core.PerforceCorePlugin;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;

/**
 * @version $Revision: 489 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
class SyncCommand extends AbstractPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the result list. */
  private List<IFile> result = new ArrayList<IFile>();

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Call this method to create a new SyncCommand instance.
   *
   * @param monitor the progress monitor.
   *
   * @return the newly created SyncCommand.
   */
  public static final SyncCommand newInstance(IProgressMonitor monitor) {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new SyncCommand(status, monitor);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Constructor for SyncCommand.
   *
   * @param status the status object.
   * @param monitor the progress monitor.
   */
  private SyncCommand(MultiStatus status, IProgressMonitor monitor) {
    super(status, monitor);

  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void outputInfo(char level, String infoString) {
    super.outputInfo(level, infoString);

    IWorkspaceRoot root = PerforceCorePlugin.getWorkspace().getRoot();

    int pos = infoString.indexOf("- added as");
    if (pos != -1) {
      String file = infoString.substring(pos + 11);
      getProgressMonitor().subTask("Adding " + getDisplayName(file));

      IFile fileHandle = root.getFileForLocation(new Path(file));
      if (fileHandle != null) {
        result.add(fileHandle);
      }

      return;
    }

    pos = infoString.indexOf("- updating");
    if (pos != -1) {
      String file = infoString.substring(pos + 11);
      getProgressMonitor().subTask("Updating " + getDisplayName(file));

      IFile fileHandle = root.getFileForLocation(new Path(file));
      if (fileHandle != null) {
        result.add(fileHandle);
      }

      return;
    }

    pos = infoString.indexOf("- deleted as");
    if (pos != -1) {
      String file = infoString.substring(pos + 13);
      getProgressMonitor().subTask("Deleting " + getDisplayName(file));

      IFile fileHandle = root.getFileForLocation(new Path(file));
      if (fileHandle != null) {
        result.add(fileHandle);
      }

      return;
    }
  }

  public String getCommand() {
    return "sync"; //$NON-NLS-1$
  }

  /**
   * Call this method to retrieve the result list.
   *
   * @return the result list.
   */
  public List<IFile> getResult() {
    return result;
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /**
   * Called to generate the display name for the given file string.
   *
   * @return name to display.
   */
  private String getDisplayName(String file) {
    IPath path = new Path(file);
    String[] segments = path.segments();
    if (segments.length < 4) {
      return file;
    }

    StringBuilder result = new StringBuilder("...");
    for (int i = segments.length - 3; i < segments.length; i++) {
      result.append(File.separatorChar).append(segments[i]);
    }

    return result.toString();
  }
}
